/*
 * Created on 19/lug/07
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package it.unibo.deis.lia.mmhc.test.logger;

import java.io.*;
import java.util.*;

public class Mobilware08 {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		new Mobilware08().parse();
	}

	void parse() throws Exception {
		FileReader fr = new FileReader("./log/MobilWare08_1db_3ms.txt");
		BufferedReader br = new BufferedReader(fr);
		String line;

		double currentActualSpeed = 0.0;
		int currentPreceivedState = 0;
		long lastActualStateChange = -1;
		long lastPerceivedStateChange;

		int actualStateChangeAmount = 0;
		int perceivedStateChangeAmount = 0;

		// long deltaAcc=0;
		// int deltaAmount=0;
		Vector<Long> deltaV = new Vector<Long>();

		int right = 0;
		int wrong = 0;
		int unavailable = 0;
		int rightAfterSeconds = 0;
		int wrongAfterSeconds = 0;

		long startTime = -1;
		long currentTime = -1;

		while ((line = br.readLine()) != null) {
			String[] tokens = line.split("[ ]");
			currentTime = Long.parseLong(tokens[1]);
			if (startTime == -1) {
				startTime = currentTime;
			}
			String type = tokens[5];
			String mess = tokens[6];
			if (type.equals("Speed")) {
				// actual state
				double tempSpeed = Double.parseDouble(mess);
				if (tempSpeed != currentActualSpeed) {
					actualStateChangeAmount++;
					lastActualStateChange = currentTime;
					currentActualSpeed = tempSpeed;
				}
			} else if (type.equals("State")) {
				// perceived state
				int tempState = Integer.parseInt(mess);
				if (tempState != currentPreceivedState) {
					perceivedStateChangeAmount++;
					lastPerceivedStateChange = currentTime;
					currentPreceivedState = tempState;
					if (lastActualStateChange != -1) {
						System.out
								.println("delta "
										+ (lastPerceivedStateChange - lastActualStateChange));
						// deltaAcc=deltaAcc+(lastPerceivedStateChange-lastActualStateChange);
						// deltaAmount++;
						deltaV.addElement(new Long(lastPerceivedStateChange
								- lastActualStateChange));
					}
				}
				if (currentPreceivedState == -1) {
					unavailable++;
				} else if (currentActualSpeed > 0 && currentPreceivedState == 1
						|| currentActualSpeed == 0
						&& currentPreceivedState == 0) {
					right++;
					if (currentTime - lastActualStateChange > 5000) {
						rightAfterSeconds++;
					}
				} else {
					wrong++;
					if (currentTime - lastActualStateChange > 5000) {
						wrongAfterSeconds++;
					}
				}
			} else {
				System.out.println("Unknown message type: " + type);
			}
		}

		System.out.println();
		System.out.println();
		System.out.println("Simulation length: " + (currentTime - startTime)
				/ 1000.0 + " seconds == "
				+ (float) ((currentTime - startTime) / 1000.0 / 60.0)
				+ " minutes");
		System.out.println();
		System.out.println("actualStateChangeAmount: "
				+ actualStateChangeAmount);
		System.out.println("unavailable: " + unavailable);
		System.out.println("right: " + right);
		System.out.println("wrong: " + wrong);
		int total = right + wrong;
		System.out.println("total avilable: " + total);
		float hit = (float) right / (float) total;
		System.out.println("hit: " + hit);
		System.out.println();
		System.out.println("rightAfterSeconds: " + rightAfterSeconds);
		System.out.println("wrongAfterSeconds: " + wrongAfterSeconds);
		int totalAfterSeconds = rightAfterSeconds + wrongAfterSeconds;
		System.out.println("totalAfterSeconds avilable: " + totalAfterSeconds);
		float hitAfterSeconds = (float) rightAfterSeconds
				/ (float) totalAfterSeconds;
		System.out.println("hitAfterSeconds: " + hitAfterSeconds);
		System.out.println();
		// System.out.println("deltaAcc: "+deltaAcc);
		// System.out.println("deltaAmount: "+deltaAmount);
		// float responsiveness=(float)deltaAcc/(float)deltaAmount;
		System.out.println("responsiveness average: " + computeAverage(deltaV));
		System.out.println("responsiveness standard deviation: "
				+ computeStandardDeviation(deltaV));
	}

	private float computeAverage(Vector<Long> v) {
		int count = 0;
		long somm = 0;
		for (int i = 0; i < v.size(); i++) {
			if (v.elementAt(i) != null) {
				somm += v.elementAt(i);
				count++;
			}
		}
		float average = (float) somm / (float) count;
		return average;
	}

	private float computeStandardDeviation(Vector<Long> v) {
		int count = 0;
		float somm = 0;
		for (int i = 0; i < v.size(); i++) {
			if (v.elementAt(i) != null) {
				somm += v.elementAt(i);
				count++;
			}
		}
		float average = somm / (float) count;
		// System.out.println("average: "+average+"  (value:"+count+")");

		count = 0;
		somm = 0;
		for (int i = 0; i < v.size(); i++) {
			if (v.elementAt(i) != null) {
				somm += Math.pow(v.elementAt(i) - average, 2);
				count++;
			}
		}
		float standardDeviation = (float) Math.sqrt(somm / count);
		// System.out.println("standardDeviation: "+standardDeviation+"  (value:"+count+")");

		return standardDeviation;
	}
}
